﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace SchEdu.Framework.DataModel
{
    public class OperationKey : IComparable
    {
        SchEduDataSet.OperationRow operation;

        public SchEduDataSet.OperationRow Operation
        {
            get { return operation; }
        }

        public int OperationID
        {
            get { return operation.ID; }
        }

        public int OrderID
        {
          get { return operation.OrderID; }
        }

        public int SequenceNo
        {
          get { return operation.SequenceNo; }
        }

        public OperationKey(SchEduDataSet.OperationRow operation)
        {
            this.operation = operation;
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            OperationKey key = obj as OperationKey;
            if (key == null)
            {
                throw new ArgumentException("Object is not OperationKey");
            }
            if (this.operation.OrderID < key.OrderID)
            {
                return -1;
            }
            if (this.operation.OrderID > key.OrderID)
            {
                return 1;
            }
            if (this.operation.SequenceNo < key.SequenceNo)
            {
                return -1;
            }
            if (this.operation.SequenceNo > key.SequenceNo)
            {
                return 1;
            }
            if (this.operation.ID > key.OperationID)
            {
                return 1;
            }
            if (this.operation.ID < key.OperationID)
            {
                return -1;
            }
            return 0;
        }

        #endregion
    }
    

    public class OperationSortedList : System.Collections.Generic.SortedList<OperationKey, SchEduDataSet.OperationRow>
    {
        public OperationSortedList()
            : base()
        {
        }

        public OperationSortedList(OperationList operations)
        {
            foreach(SchEduDataSet.OperationRow operation in operations)
            {
                this.Add(new OperationKey(operation), operation);
            }
        }

        public OperationSortedList(SchEduDataSet.OperationRow[] operations)
        {
            foreach (SchEduDataSet.OperationRow operation in operations)
            {
                this.Add(new OperationKey(operation), operation);
            }
        }

        public OperationSortedList(SchEduDataSet.OperationDataTable operations)
        {
            foreach (SchEduDataSet.OperationRow operation in operations)
            {
                this.Add(new OperationKey(operation), operation);
            }
        }

        public void Add(SchEduDataSet.OperationRow operation)
        {
            this.Add(new OperationKey(operation), operation);
        }

        public SchEduDataSet.OperationRow FindByID(int ID)
        {
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                if (item.Value.ID == ID)
                {
                    return item.Value;
                }
            }
            return null;
        }

        public int GetProcessingTime(SchEduDataSet.OrderRow order)
        {
            int processingTime = 0;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                if (item.Value.OrderID == order.ID)
                {
                    processingTime += order.ProcessingTime;
                }
            }
            return processingTime;
        }

        public int GetProcessingTime()
        {
            int processingTime = 0;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                    processingTime += item.Value.ProcessingTime;
            }
            return processingTime;
        }

        public int GetStartDate(SchEduDataSet.OrderRow order)
        {
            int startDate = 0;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                if (item.Value.OrderID == order.ID)
                {
                    startDate = Math.Min(startDate, item.Value.StartDate);
                }
            }
            return startDate;
        }

        public int GetStartDate()
        {
            int startDate = 0;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                startDate = Math.Min(startDate, item.Value.StartDate);
            }
            return startDate;
        }

        public int GetEndDate(SchEduDataSet.OrderRow order)
        {
            int endDate = 0;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                if (item.Value.OrderID == order.ID)
                {
                    endDate = Math.Max(endDate, item.Value.EndDate);
                }
            }
            return endDate;
        }

        public int GetEndDate()
        {
            int endDate = 0;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                endDate = Math.Max(endDate, item.Value.EndDate);
            }
            return endDate;
        }


        public SchEduDataSet.OperationRow MinProcessingTime(SchEduDataSet.OrderRow order)
        {
            SchEduDataSet.OperationRow minOperation = null;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                if (item.Value.OrderID != order.ID)
                {
                    continue;
                }
                if (minOperation == null)
                {
                    minOperation = item.Value;
                    continue;
                }
                if (item.Value.ProcessingTime < minOperation.ProcessingTime)
                {
                    minOperation = item.Value;
                }
            }
            return minOperation;
        }

        public SchEduDataSet.OperationRow MinProcessingTime()
        {
            SchEduDataSet.OperationRow minOperation = null;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                if (minOperation == null)
                {
                    minOperation = item.Value;
                    continue;
                }
                if (item.Value.ProcessingTime < minOperation.ProcessingTime)
                {
                    minOperation = item.Value;
                }
            }
            return minOperation;
        }

        public SchEduDataSet.OperationRow MaxProcessingTime(SchEduDataSet.OrderRow order)
        {
            SchEduDataSet.OperationRow maxOperation = null;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                if (item.Value.OrderID != order.ID)
                {
                    continue;
                }
                if (maxOperation == null)
                {
                    maxOperation = item.Value;
                    continue;
                }
                if (item.Value.ProcessingTime > maxOperation.ProcessingTime)
                {
                    maxOperation = item.Value;
                }
            }
            return maxOperation;
        }

        public SchEduDataSet.OperationRow MaxProcessingTime()
        {
            SchEduDataSet.OperationRow maxOperation = null;
            foreach (KeyValuePair<OperationKey, SchEduDataSet.OperationRow> item in this)
            {
                if (maxOperation == null)
                {
                    maxOperation = item.Value;
                    continue;
                }
                if (item.Value.ProcessingTime > maxOperation.ProcessingTime)
                {
                    maxOperation = item.Value;
                }
            }
            return maxOperation;
        }

    }

}
